package shiro.redis;

import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

import org.apache.shiro.cache.Cache;

public class RedisCache<K, V> implements Cache<K, V> {
	 
    //private Logger logger = LoggerFactory.getLogger(this.getClass());
 
    //private String KEY_SET_NAME = "redis_cache_key";
 
    //private String keyPrefix = "shiro_session:";
    
 
    @Override
    public V get(K key) 
    {
        return RedisManager.get(key);
    }
    
    @Override
    public V put(K key, V value) 
    {
    	return RedisManager.put(key, value);
    }
 
    @Override
    public V remove(K key) 
    {
    	return RedisManager.remove(key);
    }
 
    @Override
    public void clear() 
    {
    	RedisManager.flushDB();
    }
 
    @Override
    public int size() 
    {
        return RedisManager.dbSize().intValue();
    }
 
    @Override
    public Set<K> keys() {
        /*try {
            Set<byte[]> keys = RedisManager.keys(this.keyPrefix + "*");
            if (CollectionUtils.isEmpty(keys)) {
                return Collections.emptySet();
            } else {
                Set<K> newKeys = new HashSet<K>();
                for (byte[] key : keys) {
                    newKeys.add((K) key);
                }
                return newKeys;
            }
        } catch (Throwable t) {
            throw new CacheException(t);
        }*/
    	return new HashSet<K>();
    }

    @Override
    public Collection<V> values() 
    {
    	return null;
        /*try {
            Set<byte[]> keys = RedisManager.keys(this.keyPrefix + "*");
            if (!CollectionUtils.isEmpty(keys)) {
                List<V> values = new ArrayList<V>(keys.size());
                for (byte[] key : keys) {
                    @SuppressWarnings("unchecked") V value = get((K) key);
                    if (value != null) {
                        values.add(value);
                    }
                }
                return Collections.unmodifiableList(values);
            } else {
                return Collections.emptyList();
            }
        } catch (Throwable t) {
            throw new CacheException(t);
        }*/
    }
    
    /*private byte[] getByteKey(K key) 
    {
        if (key instanceof String) 
        {
            String preKey = this.keyPrefix + key;
            return preKey.getBytes();
        } else if(key instanceof PrincipalCollection){
            String preKey = this.keyPrefix + key.toString();
            return preKey.getBytes();
        }else{
            return SerializeUtils.valueToBytes(key);
        }
    }*/
 
}

